package com.fenxiangtech.mall.web.controller.v1;

import com.fenxiangtech.mall.entity.Product;
import com.fenxiangtech.mall.service.ProductService;
import com.fenxiangtech.mall.dto.PageData;
import com.fenxiangtech.mall.dto.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhangjiashun
 */
@Controller("productControllerV1")
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/recent")
    @ResponseBody
    public List<Product> listRecentProduct(@RequestParam("count") Integer count) {
        return productService.listRecentProduct(count);
    }

    @GetMapping("/by_category")
    @ResponseBody
    public List<Product> getAllInCategory(@RequestParam("id") Long id) {
        return productService.listProductByCategoryID(id);
    }

    @GetMapping("/get")
    @ResponseBody
    public Product getOne(@RequestParam("id") Long id) {
        return productService.getProductByID(id);
    }

    @GetMapping("/list")
    @ResponseBody
    public PageData<Product> listProduct(Integer pageIndex, Integer pageSize) {
        return productService.listProduct(pageIndex, pageSize);
    }

    @PostMapping("/delete")
    @ResponseBody
    public Result<String> deleteProduct(@RequestParam("id") Long id) {
        return productService.deleteProduct(id);
    }

}
